// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.ScanShipment;
import com.fowo.api.model.scan.shipment.ScanShipmentImportPo;
import com.fowo.api.model.scan.shipment.ScanShipmentItemVo;
import com.fowo.api.model.scan.shipment.ScanShipmentSearchParamPo;
import com.fowo.api.model.scan.shipment.ScanShipmentVo;
import com.fowo.api.service.ScanShipmentService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "扫码发货")
@RequestMapping("/api/scan-shipment")
@RestController
public class ScanShipmentController {

  @Resource
  private ScanShipmentService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<ScanShipmentItemVo>> search(ScanShipmentSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<ScanShipmentItemVo>>> searchOptions(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(ScanShipmentSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<ScanShipmentItemVo>> getPage(ScanShipmentSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(ScanShipmentSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<ScanShipmentVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取系统单号相关的编号列表")
  @GetMapping("order-id/{texts}/ids")
  public R<List<Long>> getIdsByOrderId(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByOrderId(nameList));
  }

  @Operation(summary = "获取编号相关的系统单号列表")
  @GetMapping("order-id/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getOrderIdByIds(idList));
  }

  @PreAuthorize("@am.canAccess('scanShipmentCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody ScanShipmentVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('scanShipmentEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody ScanShipmentVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('scanShipmentEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody ScanShipmentVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('scanShipmentDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('scanShipmentDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('scanShipmentImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<ScanShipmentImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('scanShipmentImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<ScanShipmentImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(response, "扫码发货导入模版.xlsx");
    InputStream inputStream = new ClassPathResource(
      "templates/ScanShipment.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('scanShipmentExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    ScanShipmentSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @Operation(summary = "一单一件验货")
  @GetMapping("order-one-product")
  public R<List<ScanShipmentItemVo>> orderOneProduct(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderOneProduct(search));
  }

  @Operation(summary = "一单一件验货分页")
  @GetMapping("order-one-productPage")
  public R<Page<ScanShipmentItemVo>> orderOneProductPage(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderOneProductPage(search));
  }

  @Operation(summary = "一单一件验货最后更新时间")
  @GetMapping("order-one-productLastUpdateTime")
  public R<Date> orderOneProductLastUpdateTime(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderOneProductLastUpdateTime(search));
  }

  @Operation(summary = "一单多件验货")
  @GetMapping("order-more-product")
  public R<List<ScanShipmentItemVo>> orderMoreProduct(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderMoreProduct(search));
  }

  @Operation(summary = "一单多件验货分页")
  @GetMapping("order-more-productPage")
  public R<Page<ScanShipmentItemVo>> orderMoreProductPage(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderMoreProductPage(search));
  }

  @Operation(summary = "一单多件验货最后更新时间")
  @GetMapping("order-more-productLastUpdateTime")
  public R<Date> orderMoreProductLastUpdateTime(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderMoreProductLastUpdateTime(search));
  }

  @Operation(summary = "称重")
  @GetMapping("weigh")
  public R<List<ScanShipmentItemVo>> weigh(ScanShipmentSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.weigh(search));
  }

  @Operation(summary = "称重分页")
  @GetMapping("weighPage")
  public R<Page<ScanShipmentItemVo>> weighPage(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.weighPage(search));
  }

  @Operation(summary = "称重最后更新时间")
  @GetMapping("weighLastUpdateTime")
  public R<Date> weighLastUpdateTime(ScanShipmentSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.weighLastUpdateTime(search));
  }

  @Operation(summary = "发货")
  @GetMapping("shipment")
  public R<List<ScanShipmentItemVo>> shipment(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.shipment(search));
  }

  @Operation(summary = "发货分页")
  @GetMapping("shipmentPage")
  public R<Page<ScanShipmentItemVo>> shipmentPage(
    ScanShipmentSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.shipmentPage(search));
  }

  @Operation(summary = "发货最后更新时间")
  @GetMapping("shipmentLastUpdateTime")
  public R<Date> shipmentLastUpdateTime(ScanShipmentSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.shipmentLastUpdateTime(search));
  }
}
